package com.pinyougou.service;

import com.pinyougou.pojo.TbSeller;
import com.pinyougou.sellergoods.service.SellerService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.ArrayList;
import java.util.List;

/**
 * 认证类
 * <p>title:com.pinyougou.service</p>
 * name:orang
 * date:2018/9/21 0021-14:44
 */
public class UserDatilsServiceImpl implements UserDetailsService {
    /**
     * 认证类调用服务方法
     */
    private SellerService sellerService;

    public void setSellerService(SellerService sellerService) {
        this.sellerService = sellerService;
    }

    /**
     * 认证的方法
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println("经过了loadUserByUsername");
        //构建角色列表
        List<GrantedAuthority> grantedAutho = new ArrayList<GrantedAuthority>();
        grantedAutho.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        //得到商家对象
        TbSeller seller = sellerService.findOne(username);

        if (seller != null){
            if (seller.getStatus().equals("1")){
                return new User(username,seller.getPassword(),grantedAutho);
            }else {
                return null;
            }
        }else {
                return null;
        }
    }
}
